package com.heima.wemedia.interceptor;

import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.thread.USerThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author :28169
 * @date : 2022/7/15 14:52
 */
@Slf4j
public class AuthInterceptor  implements HandlerInterceptor {

    //前置拦截器
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //1.获取请求头上的userId字段
        String userId = request.getHeader("userId");
        if (StringUtils.isEmpty(userId)){
            log.warn("用户id空");
            return false;
        }
        //2.jianguserId写入到ThreadLocal
        WmUser wmUser = new WmUser();
        wmUser.setId(Integer.parseInt(userId));
        USerThreadLocal.set(wmUser);

        return true;
    }
}
